layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func', 'tree'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var tree = layui.tree;

    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable" ,   //表格id
        condition: {
            deptId: ""
        }
    };
    /**
     * 检索店名
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyWage/getDm");
    var languageResult = languageAjax.start();
    $.each(languageResult, function (index, val) {
        $("#shopName").append("<option value='" + val.dm + "'>" + val.dm + "</option>");
    });
    form.render('select');


    /**
     * 检索职务
     */
    var languageAjax = new $ax(Feng.ctxPath + "/btyWx/getPost");
    var languageResult = languageAjax.start();
    $.each(languageResult, function (index, val) {
        $("#post").append("<option value='" + val.zw + "'>" + val.zw + "</option>");
    });
    form.render('select');

    /**
     * 检索区域
     */
    var qyAjax = new $ax(Feng.ctxPath + "/btyWx/getRegionOne");
    var qyResult = qyAjax.start();
    $.each(qyResult, function (index, val) {
        $("#regionOne").append("<option value='" + val.qy + "'>" + val.qy + "</option>");
    });
    form.render('select');

    form.on('select(regionOne)', function (data) {
        var regionOne = data.value;
        if (regionOne == "") {
            $("#regionTwo").html("");
            $("#regionTwo").append("<option value=''>全部</option>")
            form.render('select');
        } else {
            var qyAjax = new $ax(Feng.ctxPath + "/btyWx/getRegionTwo");
            qyAjax.set("region", regionOne);
            var qyResult = qyAjax.start();
            $("#regionTwo").html("");
            $.each(qyResult, function (index, val) {
                $("#regionTwo").append("<option value='" + val.eqy + "'>" + val.eqy + "</option>");
            });
            form.render('select');
        }
    });
 

    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', align: "left", hide: true, sort: true, title: 'id'},
            {field: 'shopName', align: "left", sort: true, title: '店铺名'},
            {field: 'name', align: "left", sort: false, title: '姓名',width:70},
            {field: 'post', align: "left", sort: false, title: '职务',width:70},
            {field: 'deptName', align: "left", sort: false, title: '部门',width:70},
            {field: 'openid', align: "left", sort: true, title: '微信openid'},
            {field: 'dingId', align: "left", sort: false, title: '钉钉ID'},
            {field: 'nickname', align: "left", sort: true, title: '微信昵称'},
            {field: 'phone', align: "left", sort: false, title: '手机号'},
            {field: 'regionOne', align: "left", sort: false, title: '区域',width:80},
            {field: 'regionTwo', align: "left", sort: false, title: '二级区域',width:80},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];

    };
    // 重置按钮事件
    $('#btnReset').click(function () {
        $('input').val('');
        $("#post").val('');
        $("#regionOne").val('');
        $("#regionTwo").val('');
        $("#shopName").val('');
        form.render();
    });

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['name'] = $("#name").val();
        queryData['phone'] = $("#phone").val();
        queryData['post'] = $("#post").val();
        queryData['shopName'] = $("#shopName").val();
        queryData['regionOne'] = $("#regionOne").val();
        queryData['regionTwo'] = $("#regionTwo").val();
        queryData['deptId'] = Notice.condition.deptId;
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加通知
     */
    Notice.openAddNotice = function () {
        func.open({
            height: 700,
            title: '添加关联关系',
            content: Feng.ctxPath + '/btyWx/btyWx_add',
            tableId: Notice.tableId
        });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/notice/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("noticeId", data.noticeId);
            ajax.start();
        };
        Feng.confirm("是否删除通知 " + data.title + "?", operation);
    };
    /**
     * 点击修改
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {
        func.open({
            height: 700,
            title: '修改档案',
            content: Feng.ctxPath + "/btyWx/btyWx_update/" + data.id,
            tableId: Notice.tableId
        });
    };

    // 渲染表格
    Notice.tableResult = {
        elem: '#' + Notice.tableId,
        data:[],
        even: true,
        // url: Feng.ctxPath + '/btyWx/list',
        page: true,
        cellMinWidth: 100,
        limit: 20,
        height: "full-65",
        cols: Notice.initColumn(),
    };
    Notice.tableResult.url=Feng.ctxPath + '/btyWx/list';
    setTimeout(()=>{
        table.render(Notice.tableResult);
    },1000);
    /**
     * 选择部门时
     */
    Notice.onClickDept = function (obj) {
        Notice.condition.deptId = obj.data.id;
        Notice.search();
    };

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });

    // 部门树按钮点击事件
    $('#btnBM').click(function () {
        // leez modify
        // $("#deptTreeB").css("display","block");
        // 初始化部门树
        var ajax = new $ax(Feng.ctxPath + "/dept/layuiTree", function (data) {
            tree.render({
                elem: '#deptTree',
                data: data,
                click: Notice.onClickDept,
                onlyIconControl: true
            });
            // leez add
            $('#btnBM').css("display","none");
            let ava = document.documentElement.clientHeight
            ava = ava - 40
            document.getElementById("deptTree").style.height= ava + "px"; 
        }, function (data) {
        });
        ajax.start();
    });


    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        }
    });
});
